﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using WMSADBUtility;
using WMSAML.Warehouse;

namespace WMSADL.SY
{
    public class PD_PickingMapDAL
    {

        public PD_PickingMapEntity Getentity(string pickNO, string UserName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NID,PickNO,MapImage,PickingMapImage,UserName,CreatTime,UpdateTime,Field1,Field2,Field3,Field4,Field5 from PD_PickingMap ");
            strSql.Append(" where pickNO='" + pickNO + "' AND UserName='" + UserName + "' ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            PD_PickingMapEntity entity = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    entity = ReaderBind(dataReader);
                }
            }
            return entity;
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(PD_PickingMapEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into PD_PickingMap(");
            strSql.Append("PickNO,MapImage,PickingMapImage,UserName,CreatTime,UpdateTime,Mark,field1,field2,field3,field4,field5)");

            strSql.Append(" values (");
            strSql.Append("@PickNO,@MapImage,@PickingMapImage,@UserName,@CreatTime,@UpdateTime,@Mark,@field1,@field2,@field3,@field4,@field5)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PickNO", DbType.String, entity.PickNO);
            db.AddInParameter(dbCommand, "MapImage", DbType.Binary, entity.MapImage);
            db.AddInParameter(dbCommand, "PickingMapImage", DbType.Binary, entity.PickingMapImage);
            db.AddInParameter(dbCommand, "UserName", DbType.String, entity.UserName);
            db.AddInParameter(dbCommand, "CreatTime", DbType.DateTime, entity.CreatTime);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, entity.UpdateTime);
            db.AddInParameter(dbCommand, "Mark", DbType.String, entity.Mark);
            db.AddInParameter(dbCommand, "field1", DbType.String, entity.field1);
            db.AddInParameter(dbCommand, "field2", DbType.String, entity.field2);
            db.AddInParameter(dbCommand, "field3", DbType.String, entity.field3);
            db.AddInParameter(dbCommand, "field4", DbType.String, entity.field4);
            db.AddInParameter(dbCommand, "field5", DbType.String, entity.field5);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string PickNO, string UserCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from PD_PickingMap ");
            strSql.Append(" where PickNO='" + PickNO + "' AND UserName='" + UserCode + "' ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public PD_PickingMapEntity ReaderBind(IDataReader dataReader)
        {
            PD_PickingMapEntity entity = new PD_PickingMapEntity();
            object ojb;
            ojb = dataReader["NID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.NID = (int)ojb;
            }
            entity.PickNO = dataReader["PickNO"].ToString();
            ojb = dataReader["MapImage"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.MapImage = (byte[])ojb;
            }
            ojb = dataReader["PickingMapImage"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.PickingMapImage = (byte[])ojb;
            }
            entity.UserName = dataReader["UserName"].ToString();
            ojb = dataReader["CreatTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.CreatTime = (DateTime)ojb;
            }
            ojb = dataReader["UpdateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.UpdateTime = (DateTime)ojb;
            }
            entity.field1 = dataReader["field1"].ToString();
            entity.field2 = dataReader["field2"].ToString();
            entity.field3 = dataReader["field3"].ToString();
            entity.field4 = dataReader["field4"].ToString();
            entity.field5 = dataReader["field5"].ToString();
            return entity;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(PD_PickingMapEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PD_PickingMap set ");
            strSql.Append("PickNO=@PickNO,");
            strSql.Append("MapImage=@MapImage,");
            strSql.Append("PickingMapImage=@PickingMapImage,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("CreatTime=@CreatTime,");
            strSql.Append("UpdateTime=@UpdateTime,");
            strSql.Append("Mark=@Mark,");
            strSql.Append("field1=@field1,");
            strSql.Append("field2=@field2,");
            strSql.Append("field3=@field3,");
            strSql.Append("field4=@field4,");
            strSql.Append("field5=@field5");
            strSql.Append(" where NID=@NID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "NID", DbType.Int32, entity.NID);
            db.AddInParameter(dbCommand, "PickNO", DbType.String, entity.PickNO);
            db.AddInParameter(dbCommand, "MapImage", DbType.Binary, entity.MapImage);
            db.AddInParameter(dbCommand, "PickingMapImage", DbType.Binary, entity.PickingMapImage);
            db.AddInParameter(dbCommand, "UserName", DbType.String, entity.UserName);
            db.AddInParameter(dbCommand, "CreatTime", DbType.DateTime, entity.CreatTime);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, entity.UpdateTime);
            db.AddInParameter(dbCommand, "Mark", DbType.String, entity.Mark);
            db.AddInParameter(dbCommand, "field1", DbType.String, entity.field1);
            db.AddInParameter(dbCommand, "field2", DbType.String, entity.field2);
            db.AddInParameter(dbCommand, "field3", DbType.String, entity.field3);
            db.AddInParameter(dbCommand, "field4", DbType.String, entity.field4);
            db.AddInParameter(dbCommand, "field5", DbType.String, entity.field5);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
